<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible" width="600px" max-height="65vh">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="auto"
      v-loading="formLoading"
    >
      <el-row :gutter="20">
        <el-col :span="24">
          <el-form-item label="编目人员" prop="cataloger">
            <el-input v-model.trim="formData.cataloger" placeholder="请输入编目人员" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="编目日期" prop="catalogDate">
            <el-date-picker
              v-model="formData.catalogDate"
              type="date"
              value-format="YYYY-MM-DD"
              placeholder="请选择编目日期"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="提交审核日期" prop="submitAuditDate">
            <el-date-picker
              v-model="formData.submitAuditDate"
              type="date"
              :disabled-date="disabledDate"
              value-format="YYYY-MM-DD"
              placeholder="请选择提交审核日期"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="审批意见" prop="auditOpinion">
            <el-input
              type="textarea"
              v-model.trim="formData.auditOpinion"
              :rows="4"
              placeholder="请输入审批意见"
            />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <el-button @click="submitForm(0)" :disabled="formLoading">不同意</el-button>
      <el-button @click="submitForm(1)" type="primary" :disabled="formLoading">同意</el-button>
    </template>
  </Dialog>
</template>

<script setup lang="ts">
// 导入部分保持不变
import { CollectionImageAcquisitionApi } from '@/api/dr/collectionimageacquisition'

/** 藏品三维采集信息 表单 */
defineOptions({ name: 'CollectionAuditDialog' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('资源入账审批') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formData: any = ref({
  id: '',
  cataloger: '',
  catalogDate: '',
  submitAuditDate: '',
  auditOpinion: ''
})

// 表单验证规则 - 根据需要添加新字段的验证
const formRules = reactive({
  cataloger: [{ required: true, message: '编目人员不能为空', trigger: 'blur' }],
  catalogDate: [{ required: true, message: '编目日期不能为空', trigger: 'blur' }],
  submitAuditDate: [{ required: true, message: '提交审核日期不能为空', trigger: 'blur' }],
  auditOpinion: [{ required: true, message: '审批意见不能为空', trigger: 'blur' }]
})

const formRef = ref() // 表单 Ref

/** 禁用提交审核日期为过去的日期 */
const disabledDate = (time: Date) => {
  return time.getTime() < Date.now()
}

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  resetForm()
  formData.value.id = id
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async (sourceStatus: number) => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as any
    await CollectionImageAcquisitionApi.auditCollection({
      ...data,
      sourceStatus
    })
    message.success(t('common.actionSuccess'))
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {}
  formRef.value?.resetFields()
}
</script>

<style lang="scss" scoped>
:deep(.el-card__header) {
  padding: 12px 20px;
  background-color: #f5f7fa;
  border-bottom: 1px solid #e4e7ed;
}
</style>
